//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Input;

using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI.Internals
{
    partial class FormulaReferenceResources
    {
        public FormulaReferenceResources()
        {
            InitializeComponent();
        }
        void onMouseDown(object sender, MouseButtonEventArgs args)
        {
            var element = args.Source as FrameworkElement;
            if (element == null)
                return;
            ResizerType t = ResizeElement.GetResizerType(element);
            if (t == ResizerType.None)
                return;
            if (t == ResizerType.Move)
            {
                UIElement grid = WindowServices.GetAncestor<WorksheetGrid>(element);
                Point pt = args.MouseDevice.GetPosition(grid);
                var formulaReference = (FormulaReference)element.DataContext;
                WorksheetPane pane = (WorksheetPane) SpreadsheetElement.GetWorksheetPane(element);
                CellAddress a1 = pane.GetCellAddressAt(pt);
                CellAddress a2 = formulaReference.AsRangeAddress().TopLeft;
                ResizeElement.SetMoveOffset(element, a1 - a2);
            }
            Mouse.Capture(element);

        }
        void onMouseUp(object sender, MouseButtonEventArgs args)
        {
            if (Mouse.Captured == args.Source)
            {
                ((IInputElement) args.Source).ReleaseMouseCapture();
                Contract.Assert(Mouse.Captured == null);
            }
        }
        void onMouseMove(object sender, MouseEventArgs args)
        {
            if (Mouse.Captured != args.Source)
                return;
            Debug.WriteLine("Moving on " + sender);
            ResizerType t = ResizeElement.GetResizerType((DependencyObject)args.Source);
            var element = (FrameworkElement)args.Source;
            WorksheetPane pane = SpreadsheetElement.GetWorksheetPane(element);
            UIElement grid = WindowServices.GetAncestor<WorksheetGrid>(element);
            if (pane == null)
            {
                Debug.WriteLine("no pane");
                return;
            }
            CellAddress address = pane.GetCellAddressAt(args.GetPosition(grid));
            var formulaReference = (FormulaReference)element.DataContext;
            if (t == ResizerType.Move)
            {
                CellRelativeAddress offset = ResizeElement.GetMoveOffset(element);
                Point p2 = args.GetPosition(grid);
                address = pane.GetCellAddressAt(p2);
                address = address - offset;
                formulaReference.Move(address - formulaReference.AsRangeAddress().TopLeft );
                return;
            }
            formulaReference.Extend(address, ResizeElement.GetResizerType(element));
            pane.EnsureVisible(address);

        }

        private void onLoaded(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement) sender;
            EditedCell editedCell = EditionElement.GetEditedCell(element);
            if (editedCell != null && editedCell._wantsCapture)
            {
                ObservableCollection<FormulaReference> refs = editedCell.Formula.References;
                object dc = element.DataContext;
                if (dc == refs[refs.Count - 1])
                {
                    editedCell._wantsCapture = false;
                    Debug.WriteLine("Capturing" + element.DataContext);
                    Mouse.Capture((IInputElement) sender);
                }
            }
        }
    }
}
